// pages/editPass/editPass.js
import {
  request
} from '../../request/index.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldpass: '',
    user: {},
    http: '',
    pass: '',
    repass: '',
    reg1: false,
    reg2: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    console.log(user);
    this.setData({
      user,
      http: app.globalData.http
    });
  },
  //输入旧密码获取的值
  oldPass(e) {
    this.setData({
      oldpass: e.detail.value
    })
  },
  //输入密码获取的值
  pass(e) {
    this.setData({
      pass: e.detail.value
    })
  },
  //再次输入密码获取的值
  repass(e) {
    this.setData({
      repass: e.detail.value
    })
  },

  // 输入密码框失去焦点时验证
  passblur() {
    let reg = /^\d{1,6}$/;
    if (!reg.test(this.data.pass)) {
      this.getShowToast('请设置密码格式为六位数字');
    } else {
      this.setData({
        reg1: true
      })
    }
  },

  // 重新输入密码验证
  repassblur() {
    if (this.data.pass !== this.data.repass) {
      this.getShowToast('两次输入的密码应该一致');
    } else {
      this.setData({
        reg2: true
      })
    }
  },
  //封装后的弹框
  getShowToast(text) {
    wx.showToast({
      title: text,
      duration: 2000,
      icon: 'none'
    })
  },


  //输入验证码之后点击确定按钮触发的函数
  goClick(e) {
    if(this.data.oldpass===''||this.data.pass===''||this.data.repass===''){
      this.getShowToast('不能为空');
    }else if (this.data.oldpass != this.data.user.safetyInfo.pay_password) {
      this.getShowToast('旧密码输入不正确');
    } else if (this.data.reg1 === true && this.data.reg2 === true) {
      request({
        url:'safetyInfo/updPayPassword',
        data: {
          open_id: this.data.user.safetyInfo.open_id,
          pay_password: this.data.pass
        },
      }).then((result) => {
        console.log(result);
        if (result.data.code == '0') {
          wx.setStorageSync('user', result.data.data);
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  },
})